package org.zjh.utils;

import org.zjh.view.ui.GameUi;

import java.util.Random;

/**
 * @description: TODO
 * @author 郑佳豪
 * @date 2024/10/1 14:20
 * @version 1.0
 */
public class VerificationUtil {
    /**
     * 生成验证码
     * @return 验证码
     */
    public static  String  generateRandomVerificationCode() {
        //10个数字 26个小写字母 26个大写字母 共62个字符
        String random = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int count = 4;
        StringBuilder stringBuilder = new StringBuilder();
        int num;
        Random rand = new Random();
        for(int i=0;i<count;i++) {
            //随机获得62个字符的下标
            num = rand.nextInt(62);
            stringBuilder.append(random.charAt(num));
        }
        return stringBuilder.toString();
    }

    /**
     * 校验验证码是否正确
     * @param inputVerificationCode 用户输入的验证码
     * @return true：正确    false：错误
     */
    public static boolean checkVerificationCode(String inputVerificationCode){
        //获取登入面板中验证码标签的值 与 输入的验证码比较
        return GameUi.loginFrame.getLoginPanel().getVerificationCodeLabel().getText().equalsIgnoreCase(inputVerificationCode);
    }
}
